<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        async function fn(url, time, retry) {
            const fetchWithTimeout = (url, options, timeout) => {
                return new Promise((resolve, reject) => {
                    const timer = setTimeout(() => {
                        reject(new Error('Timeout'));
                    }, timeout);

                    fetch(url, options)
                        .then(response => {
                            clearTimeout(timer);
                            if (!response.ok) {
                                reject(new Error('Network response was not ok'));
                            }
                            resolve(response);
                        })
                        .catch(err => {
                            clearTimeout(timer);
                            reject(err);
                        });
                });
            };

            for (let i = 0; i <= retry; i++) {
                try {
                    const response = await fetchWithTimeout(url, {}, time);
                    const data = await response.json(); // 根据需要处理响应数据  
                    return data; // 返回请求的数据  
                } catch (error) {
                    if (i === retry) {
                        console.error(`请求失败: ${error.message}`);
                        throw error; // 如果达到最大重试次数，抛出错误  
                    }
                    console.warn(`请求失败，正在重试...(${i + 1}/${retry})`);
                }
            }
        }

        // 使用示例  
        fn('https://lab.lovepikachu.top/api/randpika', 5000, 3)
            .then(data => console.log(data))
            .catch(error => console.error('最终错误:', error));
    </script>
</body>

</html>